<% String title="Dazzle Tutorial using Eclipse";
   String header = title;
%>


<%@ include file="sangerheader.jsp" %>
<div id="main">

<h2>Background information on Dazzle and it's structure/methodology.</h2>
If you wish to get stuck into coding straight away jump this secion. If you wish to get an overview of the workings of Dazzle read-on.
Your dazzle servlet will be initialized through the init(..) method of the DazzleServlet java class. this sets up the data handlers that you have configured in your dazzlecfg.xml file in the form of the DazzleHandler interface.
<pre>
public interface DazzleHandler {
    public boolean accept(DazzleDataSource dds);
    
    public String[] capabilities(DazzleDataSource dds);
    
    public String[] commands(DazzleDataSource dds);
    
    public void run(
        DazzleServlet dazzle,
        DazzleDataSource dds,
        String cmd,
        HttpServletRequest req,
        DazzleResponse resp
    )
        throws IOException, ServletException, DataSourceException, DazzleException;
}
</pre>
<p>
these DazzleHandlers handle the DazzleDataSource implementations for your datasources that you have configured.
The important method here is the run(..) method that is called by the main DazzleServlet doGet(..) when a cmd is made such as features or entry_points that is not a top level cmd (such as sources and is a command) that needs to be delegated to the specific DAS source/DazzleDataSource.
</p>
<p>
the init method is below and is where any initialization errors will be thrown if your configuration file is not implemented correctly or Dazzle for some reason fails to start properly.
</p>
<pre>
/**
	 * Initialize DAS services, and load the XML configurations.
	 */
	
	public void init(ServletConfig config)
	throws ServletException
	{
		super.init(config);
				
		// Initialize handlers
		
		try {
			Set handlerNames = Services.getImplementationNames(
					DazzleHandler.class,
					getClass().getClassLoader()
			);
			List<Object> handlerList = new ArrayList<Object>();
			for (Iterator i = handlerNames.iterator(); i.hasNext(); ) {
				String handlerName = (String) i.next() ;
				System.out.println("adding handler " + handlerName);
				handlerList.add(getClass().getClassLoader().loadClass(handlerName).newInstance());
			}
			handlers = (DazzleHandler[] ) handlerList.toArray(new DazzleHandler[0]);
		} catch (Exception ex) {
			throw new ServletException("Error initializing handler", ex);
		}
		
		// Initialize installation
		
		String installClassName = config.getInitParameter("dazzle.installation_type");
		if (installClassName == null) {
			installClassName = DAZZLE_INSTALLATION_TYPE;
		}
		
		try {
			Class installClass = getClass().getClassLoader().loadClass(installClassName);
			installation = (DazzleInstallation) installClass.newInstance();
			installation.init(config);
		} catch (DataSourceException ex) {
			log("Error initializing installation", ex);
			throw new ServletException("Error initializing installation");
		} catch (ClassCastException ex) {
			throw new ServletException("Not a Dazzle installation: " + installClassName);
		} catch (ClassNotFoundException ex) {
			throw new ServletException("Couldn't find class: " + installClassName);
		} catch (InstantiationException ex) {
			throw new ServletException("Couldn't instantiate " + installClassName);
		} catch (IllegalAccessException ex) {
			throw new ServletException("Couldn't instantiate " + installClassName);
		}
	}
</pre>
<p>
All dazzle http get requests/DAS cmds go through the org.biojava.servlets.dazzle.DazzleServlet classes 
</p>
<h2>Sources cmd</h2>
<p>For example the sources cmd is handled directly through the DazzleServlet class, In the Main DazzleServlet.java class there is a variable called
DAS_SOURCES_LIST</p>
<pre>
public static final String DAS_SOURCES_LIST   = "/sources.xml";
</pre>
<p>
in the same class there is a sendSources(..) method which returns by default the sources.xml file contained in the dazzle-webapp dir or if you are developing in eclipse the webcontent dir of eclipse. So to edit the content of your soures.xml file you can open in any text or xml editor and edit so that it conforms to the DAS 1.6 specification (http://www.biodas.org/wiki/DAS1.6#Response). Once your sources.xml file is as it should be you can test it using the dasregistry validation page at http://www.dasregistry.org/validation.jsp and select the sources capability.
</p>
<pre>
/** DAS2- style sources description of the DAS sources, as is also supported by the DAS registry.
	 * e.g. see http://www.dasregistry.org/das1/sources/
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void sendSources(HttpServletRequest req,
			HttpServletResponse resp)
	throws ServletException, IOException
	{
		
		//TODO: automatically build up the sources description form the data-sources
		//add optional configuration fields to the dazzleconfig.xml file that allows
		//to specify the information that can not be inferred automatically
		
		System.out.println("getting sources listing from file: " +DAS_SOURCES_LIST );
		
		InputStream is = getServletContext().getResourceAsStream(DAS_SOURCES_LIST);
		
		
		resp.setContentType(XML_CONTENT_TYPE);
		
		PrintWriter pw = resp.getWriter();
		 
		if (is != null) {
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String line;
			while ((line = br.readLine()) != null) {
				pw.println(line);
			}
		}
		pw.flush();
		
		pw.close();
		
	}
	</pre>
	<h2>Entry Points cmd implementation in Dazzle:</h2>
	<p>As only DAS reference servers implement the entry_points command you need a class that implements the DazzleReferenceSource interface which specifies that getEntryPoints must be implemented. A very basic implementation is shown below:
	</p>
	<pre>
/** This method deals with the DAS -entry points command.
	 * @return a set containing the references to the entry points
	 */ 
	public Set getEntryPoints() {
		Set<String> s = new TreeSet<String> ();
		// this example has only one feature.
		// for your real data you might want to add a SQL query here.
		s.add("123");
		return s;
	}
	</pre>
	
	<h2>Sequence cmd</h2>
	<p>This is a reference source only cmd so your class must implement the DazzleReferenceSource which specifies the getSequence method below be implemented:
this method returns a Biojava Sequence object org.biojava.bio.seq.Sequence. 
</p>
Interface to be implemented:
<pre>
public Sequence getSequence(String ref)
        throws NoSuchElementException, DataSourceException;
</pre>
Example implemented method:
<pre>
public Sequence getSequence(String ref)
        throws NoSuchElementException, DataSourceException
    {
        Sequence seq = (Sequence) seqs.get(ref);
        if (seq == null) {
            throw new NoSuchElementException("No sequence " + ref);
        }
        return seq;
    }
    
</pre>

<h2>Types cmd</h2>
f you are reading all your das features into memory as with a standard GFF source does, then you would set up your a types list and then serve this list using a method such as the one below:
<p>setting up features in gff and recording types as well:</p>
<pre>
public void addRecordLine(GFFRecord record) {
      //System.out.println("allTypes adding:"+record.getFeature());
    	allTypes.add(record.getFeature());
        String seq = record.getSeqName();
        registerSeq(seq);
        List seqGFF = (List) gffSets.get(seq);
        if (seqGFF == null) {
            seqGFF = new ArrayList();
            gffSets.put(seq, seqGFF);
        }
        
        seqGFF.add(record);
    }
</pre>
then you can get types by something like the following:
<pre>
protected Set allTypes;

public Set getAllTypes() {
        return Collections.unmodifiableSet(allTypes);
    }
</pre>
<p>
note: unmodifiable method :Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.
</p>

<h2>Features Command:</h2>
from the java docs it says we should use the AbstractBiojavaFeatureSource as a starting point for writing data sources:
the important method obviously for this section is the getFeatures(String ref) method.
<pre>
/**
 * Abstract DazzleDataSource implementation which provides default implementations
 * for many methods.  This is a useful starting point for writing many
 * data source implementations.
 *
 * @author Thomas Down
 * @version 1.00
 */

public abstract class AbstractBiojavaFeatureSource 
    extends    AbstractDazzleDataSource
    implements BiojavaFeatureSource 

/**
     * Return all the features on the requested sequence
     */

    public FeatureHolder getFeatures(String ref)
        throws DataSourceException, NoSuchElementException
    {
        return getSequence(ref);
    }

</pre>
getSequence is called and looks like this:
<pre>
/**
     * Get the specified sequence.  This is used by the AbstractDataSource <code>getFeatures</code>,
     * <code>getLandmarkLength</code>, and <code>getFeaturesByID</code>
     * methods, and is a required part of the interface for DazzleReferenceSources.
     */
    
    public abstract Sequence getSequence(String ref)
        throws DataSourceException, NoSuchElementException;

this method is from the GFFAnnotationSource class which

public FeatureHolder getFeatures(String ref)
	    throws NoSuchElementException, DataSourceException
    {
        ref = mapName(ref);
     
      //System.out.println("getFeatures for " + ref);
        //System.out.println(gffSets.containsKey(ref));
        if (!gffSets.containsKey(ref)) {
            return null;
        }
        FeatureHolder features = (FeatureHolder) featureSets.get(ref);
        if (features == null) {
            try {
                Sequence seq = getSequence(ref);
                ViewSequence vseq = new ViewSequence(seq);
                annotate(vseq);
                
                features = vseq.getAddedFeatures();
                featureSets.put(ref, features);
            } catch (BioException ex) {
                throw new DataSourceException(ex, "Error annotating sequence " + ref);
            } catch (ChangeVetoException ex) {
                throw new DataSourceException("ViewSequence isn't accepting features :(");
            }	    
        } 
        return features;
    }
    </pre>
    
<h3>Handling Error Segments</h3>
 spec link is here <a href="http://www.biodas.org/wiki/DAS1.6#Exception_Handling_for_Invalid_Segments">http://www.biodas.org/wiki/DAS1.6#Exception_Handling_for_Invalid_Segments</a>
  example from class FeaturesHandler which extends AbstractDazzleHandler ( a Handler which implements one or more DAS commands).  

<pre>
private void featuresOutput_dasgff(
			HttpServletRequest req,
			DazzleResponse resp,
			BiojavaFeatureSource dds,
			Map segmentResults,
			FeatureFilter generalFilter,
			boolean categorize)
	throws IOException, ServletException, DazzleException
	{

		XMLWriter xw = resp.startDasXML("DASGFF", "dasgff.dtd");

		try {
			xw.openTag("DASGFF");
			xw.openTag("GFF");
			xw.attribute("version", DASGFF_VERSION);
			xw.attribute("href", DazzleTools.fullURL(req));

			for (Iterator i = segmentResults.entrySet().iterator(); i.hasNext(); ) {
				Map.Entry me = (Map.Entry) i.next();
				Segment seg = (Segment) me.getKey();
				Object segv = me.getValue();
				if (segv instanceof FeatureHolder) {
					FeatureHolder features = (FeatureHolder) segv;

					xw.openTag("SEGMENT");
					xw.attribute("id", seg.getReference());
					xw.attribute("version", dds.getLandmarkVersion(seg.getReference()));
					if (seg.isBounded()) {
						xw.attribute("start", "" + seg.getStart());
						xw.attribute("stop", "" + seg.getStop());
					} else {
						xw.attribute("start", "1");
						xw.attribute("stop", "" + dds.getLandmarkLength(seg.getReference()));
					}
					// TODO: Labels here?

					FeatureFilter ff = featuresOutput_buildSegmentFilter(generalFilter, seg);
					features = features.filter(ff, true);

					List nullList = Collections.nCopies(1, null);

					for (Iterator fi = features.features(); fi.hasNext(); ) {
						Feature feature = (Feature) fi.next();

						if (dds.getShatterFeature(feature)) {
							int idSeed = 1;
							String baseID = dds.getFeatureID(feature);
							List baseGroupList = dds.getGroups(feature);
							for (Iterator bi = feature.getLocation().blockIterator(); bi.hasNext(); ) {
								Location shatterSpan = (Location) bi.next();
								List groupList = new ArrayList();
								groupList.add(feature);
								groupList.addAll(baseGroupList);
								writeDasgffFeature(dds, xw, feature, groupList, shatterSpan, baseID + "-" + (idSeed++), categorize);
							}
						} else {
							List groups = dds.getGroups(feature);
							writeDasgffFeature(dds, xw, feature, groups, null, null, categorize);
						}
					}

					xw.closeTag("SEGMENT");
				} else if (segv instanceof String) {
					xw.openTag("ERRORSEGMENT");
					xw.attribute("id", seg.getReference());
					if (seg.isBounded()) {
						xw.attribute("start", "" + seg.getStart());
						xw.attribute("stop", "" + seg.getStop());
					} 
					xw.closeTag("ERRORSEGMENT");
				} else if (segv == null) {
					xw.openTag("UNKNOWNSEGMENT");
					xw.attribute("id", seg.getReference());
					xw.closeTag("UNKNOWNSEGMENT");
				}
			}

			xw.closeTag("GFF");
			xw.closeTag("DASGFF");
			xw.close();
		} catch (Exception ex) {
			throw new DazzleException(ex, "Error writing DASGFF FEATURES document");
		}
	}
</pre>

<h2>Stylesheet cmd</h2>
spec for the stylesheet cmd is here <a href="http://www.biodas.org/wiki/DAS1.6#Stylesheet_Command">http://www.biodas.org/wiki/DAS1.6#Stylesheet_Command</a>
e.g. http://localhost:8080/das/tss/stylesheet
<p>
not to be confused with the css or xsl stylesheets this stylesheet cmd should respond with a xml document for specifying how a track should be displayed in a browser example:
</p>


<pre>
&lt;DASSTYLE>
  &lt;STYLESHEET>
    &lt;CATEGORY id="default">
      &lt;TYPE id="TSS">
        &lt;GLYPH>
	  &lt;TICK>
	    &lt;HEIGHT>25&gt;HEIGHT>
	    &lt;COLOR>blue&lt;/COLOR>
	    &lt;OUTLINECOLOR>black&lt;/OUTLINECOLOR>
	  &lt;/TICK>
	&lt;/GLYPH>
      &lt;/TYPE>
    &lt;/CATEGORY>
  &lt;/STYLESHEET>
&lt;/DASSTYLE>

</pre>

the location of this is file is specified by you when you set up your source in dazzlecfg.xml. You can see the example in the TSS source that is packaged with the basic dazzle installation and looks like this: 
<pre>&lt;string name="stylesheet" value="/tss.style" /></pre>
 and it's contained in the <datasource/> element. This points to the root of the WebContent directory (if you are building in eclipse) and must have a "/" in front of it.
Of course if you wish to keep your web content directory neater you can specify a stylesheet directory within it and put it there and modify the stylesheet element of your dazzlecfg.xml source accordingly.
<pre>
&lt;datasource id="tss" jclass="org.biojava.servlets.dazzle.datasource.GFFAnnotationSource">
    &lt;string name="name" value="TSS" />
    &lt;string name="description" value="Transcription start sites" />
    &lt;string name="version" value="default" />
    &lt;string name="fileName" value="/fickett-tss.gff" />
    &lt;boolean name="dotVersions" value="true" />
    &lt;string name="mapMaster" value="http://localhost:8080/das/test/" />
    &lt;string name="stylesheet" value="/stylesheet/tss.style" />
  &lt;/datasource>
</pre> 





</div>
<%@ include file="sangerfooter.jsp" %>
